import {
  createStore
} from 'vuex'
import createPersistedState from 'vuex-persistedstate'
export default createStore({
  state: {
    userInfo: [],
    cart: {
      list: []
    }
  },
  mutations: {
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeUserInfo(state) {
      state.userInfo = []
    },
    /* 购物车添加 */
    addShopHandle(state, obj) {
      let flag = false
      state.cart.list.forEach(item => {
        if (item.skuId == obj.skuId) {
          item.count += obj.count
          flag = true
        }
      })
      if (!flag) {
        state.cart.list.unshift(obj)
      }
    }
  },
  actions: {
    getToken(ctx, userInfo) {
      ctx.commit("setUserInfo", userInfo)
    }
  },
  modules: {},
  plugins: [createPersistedState()],
})